package com.la.yuer.prc.io.chinnel;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class SocketChannelTest {


    public static void main3(String[] args) throws IOException {
        // 1、打开客户端通道
        SocketChannel socketChannel = SocketChannel.open();
        // 2、设置IP和端口交
        boolean isFlag = socketChannel.connect(new InetSocketAddress(9999));
        if(isFlag){
            System.out.println("我链接成功啦~");
        }
        //读写数数据
        socketChannel.write(ByteBuffer.wrap("老板，快来还钱啦".getBytes(StandardCharsets.UTF_8)));

        try {
            //写数据
            ByteBuffer allocate = ByteBuffer.allocate(1024);
            int read = socketChannel.read(allocate);
            System.out.println("服务端发来消息： "+new String(allocate.array(),0,read,StandardCharsets.UTF_8));
            //释放资源啦
            socketChannel.close();
        }catch (Exception e){
            e.printStackTrace();
        }




    }

    public static void main2(String[] args) throws IOException {
        while (true){
            // 1、打开客户端通道
            SocketChannel socketChannel = SocketChannel.open();
            // 2、设置IP和端口交
            boolean connectStatus  =  socketChannel.connect(new InetSocketAddress("127.0.0.1",9999));
            if(connectStatus){
                System.out.println("链接成功");
            }else
                return;
            Scanner scanner = new Scanner(System.in);
            String s = scanner.nextLine();
//            if(s.equals("stop"))
//                break;

            //读写数数据
//            socketChannel.write(ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8)));
            socketChannel.write(ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8)));

            //写数据
            ByteBuffer allocate = ByteBuffer.allocate(1024);
            int read = socketChannel.read(allocate);
            System.out.println("服务端发来消息： "+new String(allocate.array(),0,read,StandardCharsets.UTF_8));

            //释放资源啦
            socketChannel.close();
       }

    }
    public static void main(String[] args) {
        // 1、打开客户端通道
        SocketChannel socketChannel = null;
        try {

            socketChannel = SocketChannel.open();
            // 2、设置IP和端口交
            socketChannel.connect(new InetSocketAddress("127.0.0.1",9999));
            while (true){

//            if(s.equals("stop"))
//                break;

                Scanner scanner = new Scanner(System.in);
                String s = scanner.nextLine();
                //读写数数据
                socketChannel.write(ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8)));

                //写数据
                ByteBuffer allocate = ByteBuffer.allocate(1024);
                int read = socketChannel.read(allocate);
                if(read > 0){
                    System.out.println("服务端发来消息： "+new String(allocate.array(),0,read,StandardCharsets.UTF_8));
                }
                System.out.println("请输入："+socketChannel.getRemoteAddress());
//                //释放资源啦
//                socketChannel.close();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(socketChannel != null){
                //释放资源啦
                try {
                    socketChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
